<template>
  <j-modal :title="title" :width="600" :visible="visible" :confirmLoading="loading" switchFullscreen
    @cancel="handleCancel" @ok="handleOk">
    <a-form-model ref="ruleForm" :rules="rules" :model="form" :label-col="{ span: 4 }" :wrapper-col="{ span: 18 }">
      <a-form-model-item label="分类名称" prop="name">
        <a-input v-model="form.name" :allowClear=true placeholder="请输入" />
      </a-form-model-item>
    </a-form-model>
  </j-modal>
</template>
<script>
import * as interviewApi from '@/api/sjsk/help/interview.js'
export default {
  name: 'TypeModal',
  props: ['title'],
  data() {
    return {
      visible: false,
      form: { name: "" },
      loading: false,
      isAdd: true,
      rules: {
        name: [{ required: true, message: '请输入' }],
      },
    }
  },
  async created() {
  },
  methods: {
    show(type, record) {
      this.visible = true
      this.form.name = ""
      if (type == 1) this.form.parentId = "0" //添加第一层节点
      if (type == 2) this.form.parentId = record.key //添加下一层节点
      if (type == 3) { //编辑节点
        this.isAdd = false
        this.form.id = record.key
        this.form.name = record.title
      } else {
        this.isAdd = true
      }
    },
    handleCancel() {
      this.visible = false
    },
    handleOk() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          this.loading = true
          let api = ""
          if (this.isAdd) {
            api = interviewApi.addClassification(this.form)
          } else {
            api = interviewApi.updateClassification(this.form)
          }
          api.then(res => {
            if (res.success) {
              this.$message.success(res.message)
              this.$emit('ok');
              this.visible = false
              this.loading = false
            } else {
              this.loading = false
              this.$message.warn(res.message)
            }
          })
            .catch(e => {
              this.loading = false
              this.$message.warn('请求失败！')
            })
        }
      })
    }
  },
}
</script>
<style lang="less" scoped>

</style>
